<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/17
 * Time: 16:37
 */

namespace app\admin\controller;


use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;
use app\admin\model\GoodsType as GT;
class Type extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> aid = $request -> param('aid');
        $this -> token = $request -> param('token');
        $this -> page = $request -> param('page');
        $this -> typeName = $request -> param('type_name');
        $this -> typeId = $request -> param('type_id');
        $this -> hasToken = hasToken($this -> aid , $this -> token);
    }
    //列表
    public function index(){
        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $res = GT::where('type_id','>',0)->paginate(10,false,['page' => $this -> page]);
            if($res){
                return json(['status' => 0,'msg' =>ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status' => 1,'msg' =>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' =>ReturnCode::$return_code[3]]);

    }
    //添加
    public function typeadd(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> typeName)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $data = ['type_name' => $this -> typeName];
            $has = GT::where($data)->find();
            if($has){
                return json(['status' => 23,'msg'=>ReturnCode::$return_code[23]]);
            }
            $res = GT::create($data);
            if($res){
                return json(['status' => 0,'msg' =>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' =>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' =>ReturnCode::$return_code[3]]);
    }
    //详情
    public function typeinfo(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> typeId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $data = ['type_id' => $this -> typeId];
            $res = GT::where($data)->find();
            if($res){
                return json(['status' => 0,'msg' =>ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' =>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' =>ReturnCode::$return_code[3]]);
    }
    //修改
    public function typeup(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> typeId) || empty($this -> typeName)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $data = ['type_name' => $this -> typeName];
            $where = ['type_id' => $this -> typeId];
            $has = GT::where($data)->find();
            if($has){
                return json(['status' => 23,'msg'=>ReturnCode::$return_code[23]]);
            }
            $res = GT::update($data,$where);
            if($res){
                return json(['status' => 0,'msg' =>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' =>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' =>ReturnCode::$return_code[3]]);
    }
    //删除
    public function typedel(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> typeId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $data = ['type_id' => $this -> typeId];
            $res = GT::destroy($data);
            if($res){
                return json(['status' => 0,'msg' =>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' =>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' =>ReturnCode::$return_code[3]]);
    }

}